// <com.woyouqiu.Copyright>
// --------------------------------------------------------------
// <copyright>上海有求网络科技有限公司 2015</copyright>
// <Solution>Vecen.BangChuang</Solution>
// <Project>Vecen.BangChuang.Core</Project>
// <FileName>WalletDomainService.cs</FileName>
// <CreateTime>2017-05-16 17:23</CreateTime>
// <Author>何苗</Author>
// <Email>hemiao@woyouqiu.com</Email>
// <log date="2017-05-16 17:23" version="00001">创建</log>
// --------------------------------------------------------------
// </com.woyouqiu.Copyright>

using System;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.UI;
using Vecen.BangChuang.BangChuang.DomainServices.EventHandlers.EventDatas;
using Vecen.BangChuang.BangChuang.Wallets;

namespace Vecen.BangChuang.BangChuang.DomainServices.Wallets {
    public class WalletDomainService: BangChuangDomainServiceBase {

        private readonly IRepository<PointWithdraw> _pointWithdrawRepository;

        public WalletDomainService(IRepository<PointWithdraw> pointWithdrawRepository) {
            _pointWithdrawRepository = pointWithdrawRepository;
        }

        /// <summary>
        /// 审核提现申请
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public async Task AuditWithdrawApplyAsync(int id, PointWithdrawStatus status) {
            var entity = await _pointWithdrawRepository.FirstOrDefaultAsync(id);
            if(entity == null) {
                throw new UserFriendlyException("操作失败!", "提现申请已被删除!");
            }
            if(entity.Status != PointWithdrawStatus.Apply) {
                throw new UserFriendlyException("操作失败!", "提现申请已被审核过了!");
            }
            entity.AuditTime = DateTime.Now;
            entity.Status = status;
            await _pointWithdrawRepository.UpdateAsync(entity);

            EventBus.Trigger(new PointWithdrawEventData() {Point = entity.Point, WithdrawId = id});
        }
    }
}